package com.fivefu.core.camera.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.base.web.vo.ResultInfo;
import com.fivefu.core.camera.services.DbMonitorEventReportFormService;
import com.fivefu.core.camera.vo.response.MonthlySummaryPageList;
import com.fivefu.core.camera.vo.response.MonthlyUnqualifyEventBean;
import com.fivefu.core.skyeye.event.utils.VerifyDataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.fivefu.base.web.controller.BaseController;

/**
 * <p>
 * 事件报表 前端控制器
 * </p>
 *
 * @author 
 * @since 2022-09-26 17:04:37
 */
@Api(tags = "报表管理")
@RestController
@RequestMapping("/db-monitor-event-report-form")
public class DbMonitorEventReportFormController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(DbEventController.class);

    @Autowired
    private DbMonitorEventReportFormService dbMonitorEventReportFormService;

    @RequestMapping(path = "/monthlySummaryList",method = RequestMethod.POST)
    @ApiOperation(value = "月度汇总列表",httpMethod = "POST",response = ResultInfo.class,notes = "月度汇总列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "int"),
    })
    public ResultInfo monthlySummaryList(Integer year,Integer month,Integer page,Integer limit){
        try {
            if (page == null){
                return ResultInfo.renderError("页码不能为空");
            }
            if (limit == null){
                return ResultInfo.renderError("数量不能为空");
            }
            if (year == null){
                return ResultInfo.renderError("年份不能为空");
            }
            if (month == null){
                return ResultInfo.renderError("月份不能为空");
            }
            Page<MonthlySummaryPageList> listPage = dbMonitorEventReportFormService.monthlySummaryList(page,limit,year,month);
            return ResultInfo.renderSuccess(0,"查询月度汇总列表成功",listPage.getTotal(),listPage.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询月度汇总列表失败");
    }

    @RequestMapping(path = "/monthlyUnqualifyEventList",method = RequestMethod.POST)
    @ApiOperation(value = "月度不合格事件列表",httpMethod = "POST",response = ResultInfo.class,notes = "月度不合格事件列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "数量", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "eStime", value = "审核时间的开始(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),
            @ApiImplicitParam(name = "eEtime", value = "审核时间的结束(yyyy-MM-dd HH:mm:ss)", paramType = "query",required = false, dataType = "Date"),

            @ApiImplicitParam(name = "year", value = "年份", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "month", value = "月份", paramType = "query",required = true, dataType = "int"),
            @ApiImplicitParam(name = "sceneCode", value = "场景编号", paramType = "query",required = true, dataType = "String")
    })
    public ResultInfo monthlyUnqualifyEventList(Integer page,Integer limit,Integer year,Integer month,String sceneCode){
        try {
            if (page == null){
                return ResultInfo.renderError("页码不能为空");
            }
            if (limit == null){
                return ResultInfo.renderError("数量不能为空");
            }
            String eStime = StrUtils.isCheckNull(request.getParameter("eStime"));
            String eEtime = StrUtils.isCheckNull(request.getParameter("eEtime"));
            //验证时间格式
            if (StrUtils.isNotNull(eStime)){
                try {
                    VerifyDataUtils.verifyDateTime(eStime, "yyyy-MM-dd HH:mm:ss");
                    VerifyDataUtils.verifyDateTime(eEtime, "yyyy-MM-dd HH:mm:ss");
                }catch (Exception e){
                    return ResultInfo.renderError("审核时间格式错误");
                }
            }
            if (year == null){
                return ResultInfo.renderError("年份不能为空");
            }
            if (month == null){
                return ResultInfo.renderError("月份不能为空");
            }
            if (StrUtils.isEmpty(sceneCode)){
                return ResultInfo.renderError("场景编号不能为空");
            }
            Page<MonthlyUnqualifyEventBean> listPage = dbMonitorEventReportFormService.monthlyUnqualifyEventList(page,limit,eStime,eEtime,year,month,sceneCode);
            return ResultInfo.renderSuccess(0,"查询月度不合格事件列表成功",listPage.getTotal(),listPage.getRecords());
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return ResultInfo.renderError("查询月度不合格事件列表失败");
    }

    @ApiOperation(value = "月度事件数据汇总(线程)",httpMethod = "GET",response = ResultInfo.class,notes = "月度事件数据汇总(线程)")
    @RequestMapping(path = "/monthlyEventDataSummary",method = RequestMethod.GET)
    public void monthlyEventDataSummary(){
        try {
            dbMonitorEventReportFormService.monthlyEventDataSummary();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
    }
}
